package com.weipai.springboot.module.util;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.api.Response;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/31 15:21
 * 
 * 身份证认证
 */
public class IdentificaCardIdUtil {

    /**
     * 身份证认证
     * @return
     */
    public static Response idCardOCR(String img, String type){
        Response response = new Response();
        Map<String, Object> mapNew = new HashMap<>();
        try {
            String a = IdCardOCRUtil.post(type,img);
            Map<String,Object> map = JsonUtil.toMap(a);
            //判断是否成功
            if(map.get("reason").toString().equals("成功")) {
                Map<String, Object> map1 = JsonUtil.toMap( map.get("result").toString());
                //判断正反面
                String s = map1.get("side").toString();
                //map.get("side").toString().equals("front")
                if(map1.get("side").toString().equals("front")){
                    //正面
                    Map<String,String> map2 = new LinkedHashMap<>();
                    //汉字转码
                    map2.put("key",IdCardOCRUtil.APPKEY);
                    map2.put("idcard",map1.get("idcard").toString());
                    map2.put("realname",map1.get("realname").toString());
                    String result = IdCardOCRUtil.net(map2,"GET");
                    Map<String,Object> resultMap = JsonUtil.toMap(result);
                    if(resultMap.get("error_code").equals(0)){
                        Map<String, Object> resultMap1 = JsonUtil.toMap(resultMap.get("result").toString());
                        if(resultMap1.get("res").equals(1)){
                            //存储参数
                           /* p.setBirthday(map1.get("born").toString());
                            p.setIdCardNum(map1.get("idcard").toString());
                            p.setSex(map1.get("sex").toString());
                            p.setName(map1.get("realname").toString());
                            p.setNational(map1.get("nation").toString());
                            p.setResidentialAddress(map1.get("address").toString());*/

                           //认证成功
                            response.setCode(RetCode.SUCCESS);
                        }
                    }
                }else if(map1.get("side").toString().equals("back")) {
                    //背面
                    Map<String, Object> resultMap1 = JsonUtil.toMap(map.get("result").toString());
                }
            }else{
                mapNew.put("401",map.get("reason").toString());

                //认证失败
                response.setCode(RetCode.BOUND_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
//            mapNew.put("500","请求失败");
            response.setCode(RetCode.REQUEST_ERROR);
        }
        
       response.setData(mapNew);
        return response;
    }

    
    
    
}
